package 力扣;
/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */
public class 错误版本278 {
    static boolean isBadVersion(int version){
        if (version<=3){
            return false;//返回false说明是好的
        }else
            return true;//是坏的
    };
    public static int firstBadVersion(int n) {
         int low=0;int high=n-1;
         while (low<=high){
             int m=low+(high-low)/2;
             if (!isBadVersion(m)){
                 if (isBadVersion(m+1)!=false){
                     return m+1;
                 }else
                     low=m+1;
             }else {
                 high=m-1;
             }
         }
         return -1;
    }

    /**
     * 效率更高
     * @param n
     * @return
     */
    public static int firstBadVersion2(int n) {
        int l = 1, h = n;
        while (l < h) {
            int mid = l + (h - l) / 2;
            if (isBadVersion(mid)) {
                h = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    public static void main(String[] args) {
        int i = firstBadVersion(100);
        System.out.println(i);
    }

}
